ReactiveCommandBase class¶
Attributes: [DebuggerDisplay("{CanExecute}, {IsExecuting}")]
Defined in
Namespace: ReactiveUI.Reactive
Assembly: ReactiveUI.Reactive.dll
Full name: ReactiveUI.Reactive.ReactiveCommandBase<T1, T2>
Modifiers: public abstract
Summary¶
A base class for generic reactive commands.
Applies to
net10.0, net10.0-maccatalyst26.0, net10.0-desktop1.0, net10.0-browserwasm1.0, net10.0-tvos26.0, net10.0-windows10.0.19041, net10.0-macos26.0, net10.0-ios26.0, net10.0-android36.0, net9.0, net9.0-tvos18.0, net9.0-macos15.0, net9.0-maccatalyst18.0, net9.0-windows10.0.19041, net9.0-ios18.0, net9.0-desktop1.0, net8.0, net8.0-windows10.0.19041, net8.0-ios18.0, net8.0-maccatalyst18.0, net8.0-macos15.0, net8.0-tvos18.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.5, netstandard2.1, net481, net462, net471
Class hierarchy
classDiagram
class ReactiveCommandBase~T1,T2~
class IReactiveCommand~TParam, TResult~ {
<>
}
IReactiveCommand~TParam, TResult~ <|.. ReactiveCommandBase~T1,T2~
class IObservable~TResult~ {
<>
}
IObservable~TResult~ <|.. ReactiveCommandBase~T1,T2~
class IReactiveCommand {
<>
}
IReactiveCommand <|.. ReactiveCommandBase~T1,T2~
class IDisposable {
<>
}
IDisposable <|.. ReactiveCommandBase~T1,T2~
class IHandleObservableErrors {
<>
}
IHandleObservableErrors <|.. ReactiveCommandBase~T1,T2~
class ICommand {
<>
}
ICommand <|.. ReactiveCommandBase~T1,T2~
Implements: IReactiveCommand
Remarks¶
This class extends ReactiveCommand and adds generic type parameters for the parameter values passed into command execution, and the return values of command execution.
Because the result type is known by this class, it can implement IObservable. However, the implementation is defined as abstract, so subclasses must provide it.
Reactive commands encapsulate the behavior of running some execution logic and then surfacing the results on the UI
thread. Importantly, no scheduling is performed against input observables (the canExecute and execution pipelines).
To create an instance of ReactiveCommand, call one of the static creation methods defined by this class.
ReactiveCommand.Create can be used when your execution logic is synchronous.
ReactiveCommand.CreateFromObservable and
ReactiveCommand.CreateFromTask (and overloads) can be used for asynchronous
execution logic. Optionally, you can provide an observable that governs the availability of the command for execution,
as well as a scheduler to which events will be delivered.
The CanExecute property provides an observable that can be used to determine whether the command is
eligible for execution. The value of this observable is determined by both the canExecute observable provided
during command creation, and the current execution status of the command. A command that is already executing will
yield false from its CanExecute observable regardless of the canExecute observable provided
during command creation.
The IsExecuting property provides an observable whose value indicates whether the command is currently executing. This can be a useful means of triggering UI, such as displaying an activity indicator whilst a command is executing.
As discussed above, you are under no obligation to somehow incorporate this into your canExecute observable
because that is taken care of for you. That is, if the value of IsExecuting is true, the value of
CanExecute will be false. However, if the value of CanExecute is false, that does not imply
the value of IsExecuting is true.
Any errors in your command's execution logic (including any canExecute observable you choose to provide) will be
surfaced via the ThrownExceptions observable. This gives you the opportunity to handle the error before
it triggers a default handler that tears down the application. For example, you might use this as a means of alerting
the user that something has gone wrong executing the command.
For the sake of convenience, all ReactiveCommand instances are also implementations of ICommand.
This allows you to easily integrate instances of ReactiveCommand into platforms that understands ICommand
natively (such as WPF and UWP).
Constructors¶
| Name | Summary |
|---|---|
| .ctor |
Properties¶
| Name | Summary |
|---|---|
| CanExecute | Gets an observable whose value indicates whether the command can currently execute. |
| IsExecuting | Gets an observable whose value indicates whether the command is currently executing. |
| ThrownExceptions | Gets a observable which will fire whenever an exception would normally terminate ReactiveUI internal state. |
Methods¶
| Name | Summary |
|---|---|
| Dispose | |
| Subscribe | Subscribes to execution results from this command. |
| Execute | Gets an observable that, when subscribed, executes this command. |
| OnCanExecuteChanged | Will trigger a event when the CanExecute condition has changed. |
| ICommandCanExecute | Will be called by the methods from the ICommand interface. This method is called when the Command should evaluate if it can execute. |
| ICommandExecute | Will be called by the methods from the ICommand interface. This method is called when the Command should execute. |